<template>
  <div class="recommend" v-loading:[loadingText]="loading">
    <!--v-model:loading="loading"-->
    <!--无法滚动原因：容器层和内容层一样的高度-->
    <Scroll class="recommend-content">
      <!--多套一层div才能滚动-->
      <div>
        <slider v-if="sliders.length" :sliders="sliders" />
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <!--下拉刷新-->
          <van-cell
            v-for="item in albums"
            :key="item.id"
            class="item"
            @click="selectItem(item)"
          >
            <div class="icon">
              <!--<img :src="item.pic" alt="" height="60" width="60"/>-->
              <!--不使用懒加载会一次性加载所有图片-->
              <img v-lazy="item.pic" height="60" width="60" />
            </div>
            <div class="text">
              <h2 class="name">
                {{ item.username }}
              </h2>
              <p class="title">
                {{ item.title }}
              </p>
            </div>
          </van-cell>
        </div>
      </div>
    </Scroll>
    <router-view #default="{Component}">
      <transition name="slide">
        <component :is="Component" :data="selectedAlbum"/>
      </transition>
    </router-view>
  </div>
</template>

<script>
import { getRecommend } from '@/service/recommend'
import slider from '@/components/base/slider/slider.vue'
import Scroll from '@/components/base/scroll/scroll.vue'
import { ALBUM_KEY } from '@/assets/js/constant'
import { load } from '@/assets/js/array-store'

export default {
  components: {
    Scroll,
    slider
  },
  data () {
    return {
      sliders: [],
      albums: [],
      loading: false,
      finished: false,
      loadingText: '加载中666',
      selectedAlbum: load(ALBUM_KEY)
    }
  },
  created () {
    this.onLoad()
  },
  methods: {
    async onLoad () {
      this.loading = true
      const result = await getRecommend()
      this.loading = false
      this.albums = result.albums
      this.sliders = result.sliders
    },
    selectItem (album) {
      this.selectedAlbum = album
      this.cachedAlbum()
      this.$router.push({ path: `/recommend/${album.id}` })
    },
    cachedAlbum () {
      sessionStorage.setItem(ALBUM_KEY, JSON.stringify(this.selectedAlbum))
    }
  }
}
</script>

<style lang="scss" scoped>
.recommend {
  position: fixed;
  width: 100%;
  // 使元素从 top 到 bottom 的距离填满整个区域。
  // 因此，这个元素的高度将是视口高度减去 88 像素。
  top: 88px;
  bottom: 0;
  overflow: scroll;
}

.recommend-content {
  height: 100%;
}
.recommend-list {
  .list-title {
    height: 65px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: $font-size-medium;
    color: $color-theme;
  }

  .van-cell {
    background: $color-background;

    :deep(.van-cell__value) {
      display: flex;

      .icon {
        width: 60px;
        padding-right: 20px;
      }

      .text {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: flex-start;
        font-size: $font-size-medium;

        .name {
          margin-bottom: 10px;
          color: $color-text;
        }

        .title {
          color: $color-text-d;
        }
      }
    }
  }
}
</style>
